
package com.SinhalaSpellChecker;

import com.sun.star.linguistic2.XLinguServiceEventBroadcaster;
import com.sun.star.linguistic2.XLinguServiceEventListener;
import com.sun.star.linguistic2.LinguServiceEvent;
import com.sun.star.beans.XPropertySet;
import com.sun.star.beans.XPropertyChangeListener;
import com.sun.star.beans.PropertyChangeEvent;
import com.sun.star.lang.EventObject;
import com.sun.star.uno.XInterface;

import java.util.ArrayList;


/**
 *
 * This class is taken from the sample spell checker code available in the examples of the
 * OpenOffice SDK and the OpenOffice wiki
 * http://wiki.services.openoffice.org/wiki/Documentation/DevGuide/OfficeDev/Implementing_a_Spell_Checker
 */
public class PropChgHelper implements XPropertyChangeListener,XLinguServiceEventBroadcaster
{
    XInterface          xEvtSource;
    String[]            aPropNames;
    XPropertySet        xPropSet;
    ArrayList           aLngSvcEvtListeners;

    /**
     * Class constructor which initializes the XInterface and the string array which contains the
     * property names.
     */
    public PropChgHelper( XInterface xEvtSource, String[] aPropNames )
    {
        this.xEvtSource = xEvtSource;
        this.aPropNames = aPropNames;
        xPropSet        = null;
        aLngSvcEvtListeners = new ArrayList();
    }

    /**
     * Returns the XInterface object.
     * @return XInterface
     */
    public XInterface GetEvtSource()
    {
        return xEvtSource;
    }

    /**
     * Returns the XPropertySet object.
     * @return XPropertySet
     */
    public XPropertySet GetPropSet()
    {
        return xPropSet;
    }

    /**
     * Returns the string array which contains the property names.
     * @return the string arrray containing property names.
     */
    public String[] GetPropNames()
    {
        return aPropNames;
    }

    /**
     * Start the language service event listeners.
     * @parm aEvr LinguServiceEvent object
     */
    public void LaunchEvent( LinguServiceEvent aEvt )
    {
        int nCnt = aLngSvcEvtListeners.size();
        for (int i = 0;  i < nCnt;  ++i)
        {
            XLinguServiceEventListener xLstnr =
                    (XLinguServiceEventListener) aLngSvcEvtListeners.get(i);
            if (xLstnr != null)
                xLstnr.processLinguServiceEvent( aEvt );
        }
    }

    /**
     * Add property change listners to the properties in the property array.
     * @parm xPropertySet XPropertySet object
     */
    public void AddAsListenerTo( XPropertySet xPropertySet )
    {
        RemoveAsListener();           //remove all the assigned property change listners
        xPropSet = xPropertySet;
        if (xPropSet != null)
        {
            int nLen = aPropNames.length;       //get the number of properties.
            for (int i = 0;  i < nLen;  ++i)
            {
                if (aPropNames[i].length() != 0)
                {
                    try {
                        xPropSet.addPropertyChangeListener(                     //add a property change listners to each property.
                                aPropNames[i], (XPropertyChangeListener) this );
                    }
                    catch( Exception e ) {
                    }
                }
            }
        }
    }

    /**
     * Remove the assigned property change listners.
     */
    public void RemoveAsListener()
    {
        if (xPropSet != null)
        {
            int nLen = aPropNames.length;           //get the number of properties
            for (int i = 0;  i < nLen;  ++i)
            {
                if (aPropNames[i].length() != 0)
                {
                    try {
                        xPropSet.removePropertyChangeListener(                  //remove the property change listners assigned to each property
                                aPropNames[i], (XPropertyChangeListener) this );
                    }
                    catch( Exception e ) {
                    }
                }
            }

            xPropSet = null;
        }
    }


    /**
     * Remove the property change listners
     * @param  aSource EventObject object
     * @throws  com.sun.star.uno.RuntimeException
     */
    public void disposing( EventObject aSource )
            throws com.sun.star.uno.RuntimeException
    {
        if (aSource.Source == xPropSet)
        {
            RemoveAsListener();
        }
    }

   /*Not required to be implemented*/
    public void propertyChange( PropertyChangeEvent aEvt )
            throws com.sun.star.uno.RuntimeException
    {
    }

    /**
     * Add the LinguServiceEventListener to the listners in OpenOffice.
     * @param xListener LinguServiceEventListener object
     * @return boolean variable
     * @throws com.sun.star.uno.RuntimeException
     */
    public boolean addLinguServiceEventListener(
            XLinguServiceEventListener xListener )
        throws com.sun.star.uno.RuntimeException
    {
        boolean bRes = false;
        if (xListener != null)
        {
            bRes = aLngSvcEvtListeners.add( xListener );
        }
        return bRes;
    }

    /**
     * Remove the LinguServiceEventListener to the listners in OpenOffice.
     * @param xListener LinguServiceEventListener object
     * @return boolean variable
     * @throws com.sun.star.uno.RuntimeException
     */
    public boolean removeLinguServiceEventListener(
            XLinguServiceEventListener xListener )
        throws com.sun.star.uno.RuntimeException
    {
        boolean bRes = false;
        if (xListener != null)
        {
            int nIdx = aLngSvcEvtListeners.indexOf( xListener );
            if (nIdx != -1)
            {
                aLngSvcEvtListeners.remove( nIdx );
                bRes = true;
            }
        }
        return bRes;
    }

}
